/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.FrameWork.Common;

import java.util.List;

import android.app.ListFragment;
import android.content.ClipData;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.FrameWork.Entity.CustomerInfoEntity;
import com.FrameWork.Entity.DataEntity;
import com.SFA.R;
import com.SFA.OrderManagement.AuditCPRManager;

public class TitlesFragmentShow extends ListFragment {
	private String mCategory=null;
	private int mCurPosition = 0;
	private List<DataEntity> list = null;
	private CustomerInfoEntity entity = null;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		//Current position should survive screen rotations.
		if (savedInstanceState != null) {
			mCategory = savedInstanceState.getString("category");
			mCurPosition = savedInstanceState.getInt("listPosition");
		}
		entity = (CustomerInfoEntity)this.getActivity().getIntent().getSerializableExtra("CustomerInfo");
		populateTitles(mCategory,entity);
		ListView lv = getListView();
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.setCacheColorHint(Color.TRANSPARENT);
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
				final String title = (String) ((TextView) v).getText();

				// Set up clip data with the category||entry_id format.
				final String textData = String.format("%d||%d", mCategory, pos);
				ClipData data = ClipData.newPlainText(title, textData);
				v.startDrag(data, new MyDragShadowBuilder(v), null, 0);
				return true;
			}
		});

		selectPosition(mCurPosition);
	}

	private class MyDragShadowBuilder extends View.DragShadowBuilder {
		private Drawable mShadow;

		public MyDragShadowBuilder(View v) {
			super(v);

			final TypedArray a = v.getContext().obtainStyledAttributes(R.styleable.AppTheme);
			mShadow = a.getDrawable(R.styleable.AppTheme_listDragShadowBackground);
			mShadow.setCallback(v);
			mShadow.setBounds(0, 0, v.getWidth(), v.getHeight());
			a.recycle();
		}

		@Override
		public void onDrawShadow(Canvas canvas) {
			super.onDrawShadow(canvas);
			mShadow.draw(canvas);
			getView().draw(canvas);
		}
	}

	public void populateTitles(String brandCode,CustomerInfoEntity entity) {
		this.entity = entity;
		list= new AuditCPRManager(getActivity(),null).getFormList(brandCode);

		String[] items = new String[list.size()];
		for (int i = 0; i < list.size(); i++)
			items[i] = list.get(i).NAME;
		setListAdapter(new ArrayAdapter<String>(getActivity(),
				R.layout.title_list_item, items));
		mCategory = brandCode;
	}

	public void populateTitlesShow(String brandCode, CustomerInfoEntity entity){	//第一个list增加值
		this.entity = entity;
	list= new AuditCPRManager(getActivity(),null).getFormList(brandCode);

	String[] items = new String[list.size()];
	for (int i = 0; i < list.size(); i++)
		items[i] = list.get(i).NAME;
	setListAdapter(new ArrayAdapter<String>(getActivity(),
			R.layout.title_list_item, items));
	mCategory = brandCode;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		updateProductShow(position);
	}

	private void updateProductShow(int position){	//第二个list添加值的地方
		if(list== null || list.size()==0)
			return;
		/*ContentShowFragment frag = (ContentShowFragment) getFragmentManager()
                .findFragmentById(R.id.frag_show_content);
        String formcode= list.get(position).CODE;
        AuditCPRManager manager =  new AuditCPRManager(getActivity(),entity);
        List<ProductEntity> list =manager.getDataEntityByForm(formcode);
        //对过滤出来的数据做重新清理
        frag.populateTitles(list);*/
		mCurPosition = position;
	}



	public void selectPosition(int position) {
		ListView lv = getListView();
		lv.setItemChecked(position, true);
		updateProductShow(position);
	}

	@Override
	public void onSaveInstanceState (Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("listPosition", mCurPosition);
		outState.putString("category", mCategory);
	}
}
